package com.ego.admin.controller;

import com.ego.admin.po.SysMenu;
import com.ego.admin.service.SysMenuService;
import com.ego.common.annotation.ControllerEndpoint;
import com.ego.common.constant.SystemConstant;
import com.ego.common.exception.GlobalException;
import com.ego.common.util.Result;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: ego
 * @description:
 * @author: KamTang
 * @create: 2022-11-12 15:28
 **/
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController extends Administrator {

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 导航菜单
     */
    @GetMapping("/nav")
    public Result nav() {
        List<SysMenu> menuList = sysMenuService.getNavMenu(getAdmin().getUserId());
        return Result.ok().put("menuList", menuList);
    }

    /**
     * 所有菜单列表
     */
    @GetMapping("/list")
    @RequiresPermissions("sys:menu:list")
    public List<SysMenu> list() {
        List<SysMenu> menuList = sysMenuService.list();
        for (SysMenu sysMenu : menuList) {
            SysMenu parentMenuEntity = sysMenuService.getById(sysMenu.getParentId());
            if (parentMenuEntity != null) {
                sysMenu.setParentName(parentMenuEntity.getName());
            }
        }

        return menuList;
    }

    /**
     * 选择菜单(添加、修改菜单)
     */
    @GetMapping("/select")
    @RequiresPermissions("sys:menu:select")
    public Result select() {
        //查询列表数据
        List<SysMenu> menuList = sysMenuService.queryNotButtonList();

        //添加顶级菜单
        SysMenu root = new SysMenu();
        root.setMenuId(0L);
        root.setName("一级菜单");
        root.setParentId(-1L);
        root.setOpen(true);
        menuList.add(root);

        return Result.ok().put("menuList", menuList);
    }

    /**
     * 菜单信息
     */
    @GetMapping("/get/{menuId}")
    @RequiresPermissions("sys:menu:info")
    public Result get(@PathVariable("menuId") Long menuId) {
        SysMenu menu = sysMenuService.getById(menuId);
        return Result.ok().put("menu", menu);
    }

    /**
     * 保存
     */
    @ControllerEndpoint(exceptionMessage = "新增菜单失败", operation = "新增菜单")
    @PostMapping("/save")
    @RequiresPermissions("sys:menu:save")
    public Result save(@RequestBody SysMenu menu) {
        //数据校验
        verifyForm(menu);

        sysMenuService.save(menu);

        return Result.ok();
    }

    /**
     * 修改
     */
    @ControllerEndpoint(exceptionMessage = "修改菜单信息失败", operation = "修改菜单信息")
    @PostMapping("/update")
    @RequiresPermissions("sys:menu:update")
    public Result update(@RequestBody SysMenu menu) {
        //数据校验
        verifyForm(menu);
        sysMenuService.updateById(menu);
        return Result.ok();
    }

    /**
     * 删除
     */
    @ControllerEndpoint(exceptionMessage = "删除菜单失败", operation = "删除菜单")
    @PostMapping("/delete/{menuId}")
    @RequiresPermissions("sys:menu:delete")
    public Result delete(@PathVariable("menuId") long menuId) {
        if (menuId <= SystemConstant.MenuType.SYS_MENU.getCode()) {
            return Result.error("系统菜单，不能删除");
        }

        //判断是否有子菜单或按钮
        List<SysMenu> menuList = sysMenuService.gets(menuId);
        if (menuList.size() > 0) {
            return Result.error("请先删除子菜单或按钮");
        }

        sysMenuService.delete(menuId);

        return Result.ok();
    }

    /**
     * 验证参数是否正确
     */
    private void verifyForm(SysMenu menu) {
        if (StringUtils.isBlank(menu.getName())) {
            throw new GlobalException("菜单名称不能为空");
        }

        if (menu.getParentId() == null) {
            throw new GlobalException("上级菜单不能为空");
        }

        // 菜单
        if (menu.getType() == SystemConstant.MenuType.MENU.getCode()) {
            if (StringUtils.isBlank(menu.getUrl())) {
                throw new GlobalException("菜单URL不能为空");
            }
        }

        // 上级菜单类型
        int parentType = SystemConstant.MenuType.CATALOG.getCode();
        if (menu.getParentId() != 0) {
            SysMenu parentMenu = sysMenuService.getById(menu.getParentId());
            parentType = parentMenu.getType();
        }

        // 目录、菜单
        if (menu.getType() == SystemConstant.MenuType.CATALOG.getCode() ||
                menu.getType() == SystemConstant.MenuType.MENU.getCode()) {
            if (parentType != SystemConstant.MenuType.CATALOG.getCode()) {
                throw new GlobalException("上级菜单只能为目录类型");
            }
            return;
        }

        // 按钮
        if (menu.getType() == SystemConstant.MenuType.BUTTON.getCode()) {
            if (parentType != SystemConstant.MenuType.MENU.getCode()) {
                throw new GlobalException("上级菜单只能为菜单类型");
            }
        }
    }
}
